﻿using IPCLibrary;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;

namespace IPCLibraryTest
{
    
    
    /// <summary>
    ///This is a test class for SharedMemoryTest and is intended
    ///to contain all SharedMemoryTest Unit Tests
    ///</summary>
	[TestClass()]
	public class SharedMemoryTest
	{


		private TestContext testContextInstance;

		/// <summary>
		///Gets or sets the test context which provides
		///information about and functionality for the current test run.
		///</summary>
		public TestContext TestContext
		{
			get
			{
				return testContextInstance;
			}
			set
			{
				testContextInstance = value;
			}
		}

		#region Additional test attributes
		// 
		//You can use the following additional attributes as you write your tests:
		//
		//Use ClassInitialize to run code before running the first test in the class
		//[ClassInitialize()]
		//public static void MyClassInitialize(TestContext testContext)
		//{
		//}
		//
		//Use ClassCleanup to run code after all tests in a class have run
		//[ClassCleanup()]
		//public static void MyClassCleanup()
		//{
		//}
		//
		//Use TestInitialize to run code before running each test
		//[TestInitialize()]
		//public void MyTestInitialize()
		//{
		//}
		//
		//Use TestCleanup to run code after each test has run
		//[TestCleanup()]
		//public void MyTestCleanup()
		//{
		//}
		//
		#endregion


		/// <summary>
		///A test for Size
		///</summary>
		[TestMethod()]
		public void SizeTest()
		{
			string moduleName = "data.dll";
			string name = "data";
			int size = 8192;
			SharedMemory target = new SharedMemory(moduleName, name, size);
			int actual;
			actual = target.Size;
			Assert.AreEqual(size, actual, "Size was not set correctly.");
			target.Dispose();
		}

		/// <summary>
		///A test for Name
		///</summary>
		[TestMethod()]
		public void NameTest()
		{
			string moduleName = "data.dll";
			string name = "data";
			int size = 8192;
			SharedMemory target = new SharedMemory(moduleName, name, size);
			string actual;
			actual = target.Name;
			Assert.AreEqual(name, actual, "Name was not set correctly.");
			target.Dispose();
		}

		/// <summary>
		///A test for ModuleName
		///</summary>
		[TestMethod()]
		public void ModuleNameTest()
		{
			string moduleName = "data.dll";
			string name = "data";
			int size = 8192;
			SharedMemory target = new SharedMemory(moduleName, name, size);
			string actual;
			actual = target.ModuleName;
			Assert.AreEqual(moduleName, actual, "The module name was not set correctly.");
			target.Dispose();
		}

		/// <summary>
		///A test for WriteBytes
		///</summary>
		[TestMethod()]
		public void WriteBytesTest2()
		{
			string moduleName = "data.dll";
			string name = "data";
			int size = 8192;
			SharedMemory target = new SharedMemory(moduleName, name, size);

			byte[] buffer = new byte[] { 1, 2, 3, 4, 5 };
			int bufferOffset = 1;
			int dataOffset = 5;
			int count = 2;
			int expected = 2;
			int actual;
			actual = target.WriteBytes(buffer, bufferOffset, dataOffset, count);
			Assert.AreEqual(expected, actual);

			byte[] data = target.ReadBytes(0, 10);
			Assert.AreEqual(data[5], 2);
			Assert.AreEqual(data[6], 3);
			Assert.AreNotEqual(data[7], 4);

			target.Dispose();
		}

		/// <summary>
		///A test for WriteBytes
		///</summary>
		[TestMethod()]
		public void WriteBytesTest1()
		{
			string moduleName = "data.dll";
			string name = "data";
			int size = 8192;
			SharedMemory target = new SharedMemory(moduleName, name, size);

			byte[] buffer = new byte[] { 100, 200, 222 };
			int dataOffset = 500;
			int count = 2;
			int expected = 2;
			int actual;
			actual = target.WriteBytes(buffer, dataOffset, count);
			Assert.AreEqual(expected, actual);

			byte[] data = target.ReadBytes(500, 3);
			Assert.AreEqual(data[0], 100);
			Assert.AreEqual(data[1], 200);
			Assert.AreNotEqual(data[2], 222);

			target.Dispose();
		}

		/// <summary>
		///A test for WriteBytes
		///</summary>
		[TestMethod()]
		public void WriteBytesTest()
		{
			string moduleName = "data.dll";
			string name = "data";
			int size = 8192;
			SharedMemory target = new SharedMemory(moduleName, name, size);

			byte[] buffer = new byte[] { 123, 156, 189 };
			int dataOffset = 0;
			int expected = 3;
			int actual;
			actual = target.WriteBytes(buffer, dataOffset);
			Assert.AreEqual(expected, actual);

			byte[] data = target.ReadBytes(0, 3);
			Assert.AreEqual(data[0], buffer[0]);
			Assert.AreEqual(data[1], buffer[1]);
			Assert.AreEqual(data[2], buffer[2]);

			target.Dispose();
		}
		/// <summary>
		///A test for SynchronizedWrite
		///</summary>
		[TestMethod()]
		public void SynchronizedWriteTest()
		{
			string moduleName = "data.dll";
			string name = "data";
			int size = 8192;
			SharedMemory target = new SharedMemory(moduleName, name, size);

			byte[] buffer = new byte[] { 1, 2, 3, 4, 5 };
			int bufferOffset = 1;
			int dataOffset = 5;
			int count = 2;
			int expected = 2;
			int actual;
			actual = target.SynchronizedWrite(buffer, bufferOffset, dataOffset, count);
			Assert.AreEqual(expected, actual);

			byte[] data = target.ReadBytes(0, 10);
			Assert.AreEqual(data[5], 2);
			Assert.AreEqual(data[6], 3);
			Assert.AreNotEqual(data[7], 4);

			target.Dispose();
		}
		/// <summary>
		///A test for SynchronizedRead
		///</summary>
		[TestMethod()]
		public void SynchronizedReadBytes()
		{
			string moduleName = "data.dll";
			string name = "data";
			int size = 8192;
			SharedMemory target = new SharedMemory(moduleName, name, size);

			target.WriteBytes(new byte[] { 11, 22, 33, 44, 55 }, 10);

			byte[] buffer = new byte[size];
			int bufferOffset = 100;
			int dataOffset = 11;
			int count = 3;
			int expected = 3;
			int actual;
			actual = target.SynchronizedRead(buffer, bufferOffset, dataOffset, count);
			Assert.AreEqual(expected, actual);

			Assert.AreEqual(buffer[bufferOffset], 22);
			Assert.AreEqual(buffer[bufferOffset + 1], 33);
			Assert.AreEqual(buffer[bufferOffset + 2], 44);
			Assert.AreNotEqual(buffer[bufferOffset + 3], 55);

			target.Dispose();
		}

		/// <summary>
		///A test for ReadBytes
		///</summary>
		[TestMethod()]
		public void ReadBytesTest2()
		{
			string moduleName = "data.dll";
			string name = "data";
			int size = 8192;
			SharedMemory target = new SharedMemory(moduleName, name, size);

			target.WriteBytes(new byte[] { 11, 22, 33, 44, 55 }, 10);

			byte[] buffer = new byte[size];
			int bufferOffset = 100;
			int dataOffset = 11;
			int count = 3;
			int expected = 3;
			int actual;
			actual = target.ReadBytes(buffer, bufferOffset, dataOffset, count);
			Assert.AreEqual(expected, actual);

			Assert.AreEqual(buffer[bufferOffset], 22);
			Assert.AreEqual(buffer[bufferOffset+1], 33);
			Assert.AreEqual(buffer[bufferOffset+2], 44);
			Assert.AreNotEqual(buffer[bufferOffset+3], 55);

			target.Dispose();
		}

		/// <summary>
		///A test for ReadBytes
		///</summary>
		[TestMethod()]
		public void ReadBytesTest1()
		{
			string moduleName = "data.dll";
			string name = "data";
			int size = 8192;
			SharedMemory target = new SharedMemory(moduleName, name, size);

			target.WriteBytes(new byte[] { 111, 222, 113, 114, 115 }, 10);


			int dataOffset = 10;
			int count = 1;
			byte[] actual;
			actual = target.ReadBytes(dataOffset, count);

			Assert.AreEqual(actual[0], 111);
			Assert.AreEqual(actual.Length, 1);

			target.Dispose();
		}

		/// <summary>
		///A test for ReadBytes
		///</summary>
		[TestMethod()]
		public void ReadBytesTest()
		{
			string moduleName = "data.dll";
			string name = "data";
			int size = 8192;
			SharedMemory target = new SharedMemory(moduleName, name, size);

			target.WriteBytes(new byte[] { 39, 49, 59, 69, 79 }, 0);

			byte[] buffer = new byte[size];
			int dataOffset = 0;
			int count = 4;
			int expected = 4;
			int actual;
			actual = target.ReadBytes(buffer, dataOffset, count);
			Assert.AreEqual(expected, actual);

			Assert.AreEqual(buffer[0], 39);
			Assert.AreEqual(buffer[1], 49);
			Assert.AreEqual(buffer[2], 59);
			Assert.AreEqual(buffer[3], 69);
			Assert.AreNotEqual(buffer[3], 79);

			target.Dispose();
		}

		/// <summary>
		///A test for GetStream
		///</summary>
		[TestMethod()]
		public void GetStreamTest()
		{
			string moduleName = "data.dll";
			string name = "data";
			int size = 8192;
			SharedMemory target = new SharedMemory(moduleName, name, size);
			Stream actual;
			actual = target.GetStream();
			Assert.IsInstanceOfType(actual, typeof(Stream), "Did not return a stream.");
			actual.Close();
		}

		/// <summary>
		///A test for Detach
		///</summary>
		[TestMethod()]
		public void DetachTest()
		{
			string moduleName = "data.dll";
			string name = "data";
			int size = 8192;
			SharedMemory target = new SharedMemory(moduleName, name, size);
			bool expected = true;
			bool actual;
			actual = target.Detach();
			Assert.AreEqual(expected, actual);
			target.Dispose();
		}

		/// <summary>
		///A test for Attach
		///</summary>
		[TestMethod()]
		public void AttachTest()
		{
			string moduleName = "data.dll";
			string name = "data";
			int size = 8192;
			SharedMemory target = new SharedMemory(moduleName, name, size);
			bool expected = true;
			bool actual;
			actual = target.Attach();
			Assert.AreEqual(expected, actual);
			target.Dispose();
		}

		/// <summary>
		///A test for SharedMemory Constructor
		///</summary>
		[TestMethod()]
		public void SharedMemoryConstructorTest()
		{
			string moduleName = "data.dll";
			string name = "data";
			int size = 8192;
			SharedMemory target = new SharedMemory(moduleName, name, size);
			Assert.IsNotNull(target);
			target.Dispose();
		}
	}
}
